const app = getApp();

Page({
  data: {
    user: {}
  },

  onLoad() {
    this.loadUserInfo();
  },

  loadUserInfo() {
    app.request({
      url: '/login/user/checkLoginStatus',
      success: res => {
        if (res.code==1) {
          this.setData({ user: res.data });
          app.globalData.user = res.data;
          wx.setStorageSync('user', res.data);
        } else {
          wx.showToast({ title: '加载失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
  },
  navigateToHistory() {
    wx.navigateTo({
      url: '/pages/user/UserHistoryView/UserHistoryView'
    });
  },

  navigateToCodes() {
    wx.navigateTo({
      url: '/pages/user/UploadImageView/UploadImageView'
    });
  },
  updatedUser(e){
    if(this.data.user.role!="CUSTOMER"&&this.data.user.active){
      wx.showToast({ title: '管理员身份不能在这里修改信息', icon: 'none' })
      return;
    }
    const { avatarUrl } = e.detail 
    const updatedUser = {
      ...this.data.user,
      headimgurl:avatarUrl 
    };
    // 提交到后端接口
    app.request({
      url: '/user/updatePersonalInformationById',
      method: 'POST',
      data: {...updatedUser,role:null},
      success: (response) => {
        if (response.code === 1) {
          // 成功后更新页面上的 user 数据
          this.setData({ user: updatedUser });
          app.globalData.user = updatedUser;
          wx.setStorageSync('user', updatedUser);
        } else {
          wx.showToast({ title: '更新失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });
    wx.showModal({
      title: '修改昵称',
      content: '请填写昵称用于修改',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/user/EditNicknameView/EditNicknameView' // 跳转到编辑昵称页面
          });
        }
      }
    });
  },
  syncWechatUserInfo(e) {
    if(this.data.user.role!="CUSTOMER"&&this.data.user.active){
      wx.showToast({ title: '管理员身份不能在这里修改信息', icon: 'none' })
      return;
    }
    const { avatarUrl } = e.detail 
    // 更新 user 对象中的字段
    const updatedUser = {
      ...this.data.user,
      nickname:"微信用户",
      headimgurl:avatarUrl 
    };
    // 提交到后端接口
    app.request({
      url: '/user/updatePersonalInformationById',
      method: 'POST',
      data: {...updatedUser,role:null},
      success: (response) => {
        if (response.code === 1) {
          // 成功后更新页面上的 user 数据
          this.setData({ user: updatedUser });
          app.globalData.user = updatedUser;
          wx.setStorageSync('user', updatedUser);
        } else {
          wx.showToast({ title: '更新失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误', icon: 'none' });
      }
    });

    wx.showModal({
      title: '补充信息',
      content: '请填写昵称以完善资料',
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/user/EditNicknameView/EditNicknameView' // 跳转到编辑昵称页面
          });
        }
      }
    });
  },
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: res => {
        if (res.confirm) {
          app.request({
            url: '/login/logout',
            success: logoutRes => {
              wx.showToast({ title: '已退出' });
              const app = getApp();
              app.globalData.user = null;
              app.globalData.token=null;
              app.globalData.adminToken=null;
              wx.removeStorageSync('user');
              wx.removeStorageSync('token');
              wx.removeStorageSync('adminToken');
              wx.reLaunch({
                url: '/pages/login/LoginSelectView/LoginSelectView'
              });
            },
            fail:()=>{
              wx.removeStorageSync('user');
              wx.removeStorageSync('token');
              wx.removeStorageSync('adminToken');
              wx.reLaunch({
                url: '/pages/login/LoginSelectView/LoginSelectView'
              });
            }
          });
        }
      }
    });
  }
});